/*
 * @Date: 2021-08-17 17:45:03
 * @information: 日记控制层
 */
import {
  request,
  summary,
  description, // 接口名称下方的描述信息
  query, // get时参数
  path, // post, put, delete 时地址栏参数
  body, // body中的参数
  tags
} from 'koa-swagger-decorator'
import DiaryService from '../service/DiaryService'


const diaryService = new DiaryService()
const tag = tags(['Diary'])


export default class DiaryController {
  @request('post', '/diary/find/{page}/{size}')
  @summary('查询数据列表')
  @tag
  @path({
    page: { type: 'number', required: true },
    size: { type: 'number', required: true },
  })
  async findAll (ctx) {
    const pObj = ctx.validatedParams
    const data = await diaryService.findAll(pObj)
    ctx.rest(data)
  }

  @request('post', '/diary/findByUserId/{page}/{size}')
  @summary('根据用户id查询数据列表')
  @tag
  @path({
    page: { type: 'number', required: true },
    size: { type: 'number', required: true },
  })
  @body({
    userId: { type: 'string', required: true },
  })
  async findByUserId (ctx) {
    const pObj = ctx.validatedParams
    const bObj = ctx.request.body
    const data = await diaryService.findByUserId({ ...pObj, ...bObj })
    ctx.rest(data)
  }

  @request('post', '/diary/add')
  @summary('添加一条数据')
  @tag
  @body({
    content: { type: 'string', required: true },
    userId: { type: 'string', required: true },
  })
  async add (ctx) {
    const bObj = ctx.request.body
    const res = await diaryService.add(bObj)
    ctx.rest(res.id)
  }

  @request('post', '/diary/update')
  @summary('更新一条数据')
  @tag
  @body({
    id: { type: 'string', required: true },
    content: { type: 'string', required: true },
  })
  async update (ctx) {
    const bObj = ctx.request.body
    await diaryService.update(bObj)
    ctx.rest()
  }

  @request('post', '/diary/del')
  @summary('删除一条数据')
  @tag
  @body({
    id: { type: 'string', required: true },
  })
  async del (ctx) {
    const bObj = ctx.request.body
    await diaryService.del(bObj)
    ctx.rest()
  }





}


